package picturetaker.persistence;

import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import picturetaker.domain.Camera;
import picturetaker.domain.Picture;

public class ImageManager {

	private ImageBroker broker = null;

	private static ImageManager manager = null;

	private static Connection con = null;

	private ImageManager() {
		broker = new ImageBroker();
		con = Database.getConnection();
	}

	public static ImageManager getInstance() {
		if (manager == null) {
			manager = new ImageManager();
		}
		return manager;
	}

	public List<Camera> getAllCameras() throws SQLException, MalformedURLException {
		con.setAutoCommit(false);
		List<Camera> result = broker.getAllCameras();
		con.commit();
		con.setAutoCommit(true);
		return result;
	}

	public void insertCameras(List<Camera> list) throws SQLException {
		con.setAutoCommit(false);
		Iterator<Camera> iterator = list.iterator();
		while (iterator.hasNext()) {
			broker.insertCamera(iterator.next());
		}
		con.commit();
		con.setAutoCommit(true);
	}

	public void insertPicture(Picture picture) throws SQLException {
		con.setAutoCommit(false);
		broker.insertPicture(picture);
		con.commit();
		con.setAutoCommit(true);
	}
}
